﻿using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using webapp4U.BOL;

namespace webapp4U.UI.Controls
{
    public partial class Usercontrol_ProductHomePage : System.Web.UI.UserControl, ICallbackEventHandler
    {
        string error = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            //InClassKey = Globals.GetStringFromQueryString("InClassKey");

            Pager.IndexChanged += new EventHandler(PageIndex_Changed);
            Pager1.IndexChanged += new EventHandler(Page1Index_Changed);

            if (!Page.IsPostBack)
            {
                DataBindCommod();
                this.DataBind();
            }


        }
        private void PageIndex_Changed(Object sender, EventArgs e)
        {
            DataBind();
        }
        private void Page1Index_Changed(Object sender, EventArgs e)
        {
            DataBindCommod();
        }
        protected List<webapp4U.BOL.Products> GetItemsByInClassKey(string InClassKey)
        {
            List<webapp4U.BOL.Products> obj = webapp4U.BOL.Products.GetTop6Product(InClassKey, ref error);
            return obj;
        }

        public override void DataBind()
        {
            // hien thi bao nhieu hinh tren 1 trang
            Pager.PageSize = 6;

            //List<Products> obj = Products.GetProductsByCateID(CateID, Pager.PageIndex, Pager.PageSize);
            //ItemsDataList.DataSource = obj;
            //ItemsDataList.DataBind();
            List<webapp4U.BOL.InClassCategory> obj = webapp4U.BOL.InClassCategory.GetAllCategory(Pager.PageIndex, Pager.PageSize, ref error);
            rptCategory.DataSource = obj;
            rptCategory.DataBind();

            //GetItemsByInClassKey((string) Eval("InClassKey"))

            Pager.TotalRecords = webapp4U.BOL.InClassCategory.GetCountCategory(ref error);

            // kiem tra xem co can hien thi phan trang ko
            if (Pager.TotalRecords <= 1)
                DisplayPager.Visible = false;
            else
                DisplayPager.Visible = true;
        }


        protected string GetPrice(float UnitPrice)
        {
            if (UnitPrice > 0)
                return UnitPrice.ToString() + " VNĐ"; //Formatter.FormatMoney(UnitPrice)
            else
                return ResourceManager.GetString("Product_Price_Lienhe");
        }

        protected string GetButtonAddToCart(Double UnitPrice)
        {
            if (UnitPrice > 0)
                //if (Globals.LangVietNam())                
                return "<img id=btnimg src=" + Globals.ImagesUrl() + "AddToCart_nho.gif border=0 runat=server class=img />";
            //else
            //  return "<img src=" + Globals.ImagesUrl() + "AddToCart_nho_en.gif border=0 />";
            return "";
        }


        private string _callbackArg;
        void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
        {
            _callbackArg = eventArgument;
        }

        // Phương thức trả kết quả về cho client
        string ICallbackEventHandler.GetCallbackResult()
        {
            return RaiseCallbackEvent(_callbackArg);
        }

        // Nhận dữ liệu được gửi từ Client
        protected virtual string RaiseCallbackEvent(string eventArgument)
        {
            Products obj = Products.GetProductsByItemKey(eventArgument, ref error);
            ShoppingCart shop = new ShoppingCart();
            shop.AddedDate = DateTime.Now;
            shop.ItemKey = obj.ItemKey;
            shop.Description = obj.Description;
            shop.UnitPrice = obj.UnitPrice;
            shop.Quantity = 1;
            shop.Subtotal = obj.UnitPrice;

            List<ShoppingCart> ListShopCart = new List<ShoppingCart>();
            if (Session["ShoppingCart"] == null)
            {
                ListShopCart.Add(shop);
                Session["ShoppingCart"] = ListShopCart;
            }
            else
            {
                ListShopCart = (List<ShoppingCart>)Session["ShoppingCart"];
                Session["ShoppingCart"] = ShoppingCart.AddItemToShoppingCart(ListShopCart, shop);
            }

            return eventArgument; //RenderControl(obj_shop);

        }

        public string RenderControl(Control ctrl)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter tw = new StringWriter(sb);
            HtmlTextWriter hw = new HtmlTextWriter(tw);

            ctrl.RenderControl(hw);
            return sb.ToString();
        }

        protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            DataList dl = e.Item.FindControl("ItemsDataList") as DataList;
            foreach (DataListItem dli in dl.Items)
            {

                Image img = (Image)dli.FindControl("testImage");

                Label lblPrice = (Label)dli.FindControl("lblPrice");
                if (lblPrice.Text == ResourceManager.GetString("Product_Price_Lienhe"))
                {
                    img.Visible = false;
                }
                else
                {
                    HtmlInputHidden txtItemkey = (HtmlInputHidden)dli.FindControl("txtItemkey");
                    string CallBack = Page.ClientScript.GetCallbackEventReference(Page.Master.FindControl("ShoppingCart1"), "document.getElementById('" + txtItemkey.ClientID + "').value", "RateContent", "null");
                    img.Attributes["onclick"] = String.Format("javascript:{0}", CallBack);
                    img.ImageUrl = Globals.ImagesUrl() + "AddToCart_nho.gif";
                }


            }


        }

        protected void rptCategoryNotInClass_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataList dl = e.Item.FindControl("ItemsDataList") as DataList;
            foreach (DataListItem dli in dl.Items)
            {
                webapp4U.UI.Button btnAddToCart = (webapp4U.UI.Button)dli.FindControl("btnAddToCart");
                webapp4U.UI.Button btnProductOrder = (webapp4U.UI.Button)dli.FindControl("btnProductOrder");

                //Image img = (Image)dli.FindControl("testImage");

                Label lblPrice = (Label)dli.FindControl("lblPrice");
                if (lblPrice.Text == webapp4U.ResourceManager.GetString("Product_Price_Lienhe"))
                {
                    btnAddToCart.Visible = false;
                    btnProductOrder.Visible = false;
                    //img.Visible = false;
                }
                else
                {
                    HtmlInputHidden txtItemkey = (HtmlInputHidden)dli.FindControl("txtItemkey");
                    string CallBack = Page.ClientScript.GetCallbackEventReference(Page.Master.FindControl("ShoppingCart1"), "document.getElementById('" + txtItemkey.ClientID + "').value", "RateContent", "null");
                    btnAddToCart.Attributes["onclick"] = String.Format("javascript:{0}", CallBack);

                    //img.Attributes["onclick"] = String.Format("javascript:{0}", CallBack);
                    //img.ImageUrl = Globals.ImagesUrl() + "AddToCart_nho.gif";
                }


            }
        }

        protected List<webapp4U.BOL.Products> GetItemsByInCommodKey(string CommodKey)
        {
            List<webapp4U.BOL.Products> obj = webapp4U.BOL.Products.GetTop6ProductByCommod(CommodKey, ref error);
            return obj;
        }
        public void DataBindCommod()
        {
            // hien thi bao nhieu hinh tren 1 trang
            Pager1.PageSize = 6;

            //List<Products> obj = Products.GetProductsByCateID(CateID, Pager.PageIndex, Pager.PageSize);
            //ItemsDataList.DataSource = obj;
            //ItemsDataList.DataBind();
            List<webapp4U.BOL.InClassCategory> obj = webapp4U.BOL.InClassCategory.GetAllCategoryCommodNotInClass(Pager1.PageIndex, Pager1.PageSize, ref error);
            rptCategoryNotInClass.DataSource = obj;
            rptCategoryNotInClass.DataBind();

            //GetItemsByInClassKey((string) Eval("InClassKey"))

            Pager1.TotalRecords = webapp4U.BOL.InClassCategory.GetCountCategoryCommodNotInClass(ref error);

            // kiem tra xem co can hien thi phan trang ko
            if (Pager1.TotalRecords <= 1)
                DisplayPager.Visible = false;
            else
                DisplayPager.Visible = true;
        }

        protected void btn_ProductOrder_Click(object sender, EventArgs e)
        {
            //Products obj = Products.GetProductsByItemKey(ItemKey, ref error);
            //ShoppingCart shop = new ShoppingCart();
            //shop.AddedDate = DateTime.Now;
            //shop.ItemKey = obj.ItemKey;
            //shop.Description = obj.Description;
            //shop.UnitPrice = obj.UnitPrice;
            //shop.Quantity = 1;
            //shop.Subtotal = obj.UnitPrice;

            //List<ShoppingCart> ListShopCart = new List<ShoppingCart>();
            //if (Session["ShoppingCart"] == null)
            //{
            //    ListShopCart.Add(shop);
            //    Session["ShoppingCart"] = ListShopCart;
            //}
            //else
            //{
            //    ListShopCart = (List<ShoppingCart>)Session["ShoppingCart"];
            //    Session["ShoppingCart"] = ShoppingCart.AddItemToShoppingCart(ListShopCart, shop);
            //}

            //Response.Redirect(Globals.ApplicationPath + "Page/Products/shoppingcart.aspx?Action=DetailCart");
        }
    }
}